<template>
  <div class="view-fill">
    <div
      v-if="!assetDetailInfo.visible && !modifyAssetDelInfo.visible"
      class="app-container-flex"
    >
      <NewFullScreen
        v-model="showDialog"
        title="资产盘亏单明细"
        :needOperateAffair="true"
        :operateAffairData="affairTableInfo"
        @returnBack="returnBack"
      >
        <template v-slot:full-button>
          <PopoverPrint
            ref="popoverShow"
            v-model:popoverVisible="popoverVisible"
            :canPrint="$permission('assetInventoryLoss:printDetail')"
            :templateArr="templateArr"
            @tempClick="tempClick"
            @print="print"
          ></PopoverPrint>
          <el-button
            v-if="$permission('assetInventoryLoss:excel')"
            @click="exportExcel"
            >导出excel</el-button
          >
          <el-button
            v-if="$permission('assetInventoryLoss:generate') && infoList.data.canDealFlag"
            @click="modifyAssetDel"
            >生成处置单</el-button
          >
        </template>
        <div class="model-panel">
          <div class="model-panel-title">
            <p>{{ $t('basic_information') }}</p>
          </div>
          <div class="model-panel-body">
            <BasicInformation :info="infoList" />
          </div>
        </div>

        <div class="model-panel">
          <div class="model-panel-title">
            <Tooltip :text="$t('detailInfo')" />
          </div>
          <div class="model-panel-body">
            <div class="flex_full_item">
              <!-- 表格 -->
              <PageTable
                :refresh="tableInfo.refresh"
                :init-curpage="tableInfo.initCurpage"
                v-model:data="tableInfo.data"
                :pager="false"
                :summary="true"
                :check-box="false"
                :tab-index="true"
                :summary-field-list="tableInfo.summaryFieldList"
                :field-list="tableInfo.fieldList"
                :handle="tableInfo.handle"
                :isDynamic="true"
              >
                <template v-slot:col-assetName="scope">
                  <span class="slip-edit" @click="showAssetDetail(scope.row)">{{
                    scope.row.assetName
                  }}</span>
                </template>
              </PageTable>
            </div>
          </div>
        </div>
      </NewFullScreen>
    </div>
    <asset-card-detail
      v-if="assetDetailInfo.visible"
      :id="assetDetailInfo.id"
      v-model:visible="assetDetailInfo.visible"
      :title="assetDetailInfo.title"
    />
    <ModifyLoss
      v-if="modifyAssetDelInfo.visible"
      v-model:visible="modifyAssetDelInfo.visible"
      :lossId="modifyAssetDelInfo.row.id"
      :lossNo="modifyAssetDelInfo.row.orderNo"
      :id="''"
      title="新增"
      type="loss"
      :inventoryLocation="modifyAssetDelInfo.row.inventoryLocation"
      @returnModify="returnModify"
    ></ModifyLoss>
  </div>
</template>

<script setup name="assetInventoryLossDetail">
import {
  getInventoryLossOrderById,
  getInventoryLossOrderDetailCustomizeListAll,
  inventoryLossOrderValidateCanDeal,
  exportInventoryLossOrderDetail,
} from '@/api/asset.js'
import { orderOpAffairAll } from '@/api/affair'
import { getCustomizeSetting } from '@/api/customize'
import { doPrint } from '@/utils/exportUtil.js'
import { selectByCodeAndDept } from '@/api/report.js'
import ModifyLoss from '../assetDeal/modifyLoss'
import AssetCardDetail from '@/views/asset/assetManage/assetCard/details'
const { proxy } = getCurrentInstance()
const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
  orderId: {
    type: [Number, String],
  },
})
const infoList = ref({
  data: {},
  fieldList: [
    { label: '盘亏单号', value: 'orderNo', type: 'text' },
    { label: '盘点位置', value: 'inventoryLocationValue', type: 'text' },
    { label: '业务日期', value: 'businessDate', type: 'text' },
    { label: '创建人', value: 'createUserName', type: 'text' },
    { label: '创建时间', value: 'createTime', type: 'text' },
    { label: '更新人', value: 'updateUserName', type: 'text' },
    { label: '更新时间', value: 'updateTime', type: 'text' },
    { label: '备注', value: 'remark', type: 'text' },
  ],
})
// 过滤相关配置
const filterInfo = ref({
  query: {
    customizeUserVO: {},
    queryMap: {
      orderId: {
        queryType: 'EQUALS',
        queryCategory: 'AND',
        value: props.orderId,
      },
    },
    queryParams: {},
  },
})
const assetDetailInfo = ref({
  visible: false,
  id: '',
  title: '资产详情',
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  fieldList: [],
})
const affairTableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  api: orderOpAffairAll,
  query: {
    orderType: 109,
    orderId: props.orderId,
    sort: 'operate_time desc',
  },
  fieldList: [
    { label: '操作人', value: 'operateUserName' },
    { label: '操作部门', value: 'operateDeptName' },
    { label: '操作时间', value: 'operateTime' },
    { label: '操作类型', value: 'operateTypeValue' },
    { label: '备注', value: 'remark' },
  ],
})
const modifyAssetDelInfo = ref({
  visible: false,
  row: {},
})
const showDialog = computed({
  get() {
    return props.visible
  },
  set(val) {
    proxy.$emit('update:visible', val) // 更新父组件visible
  },
})

function returnBack() {
  proxy.$emit('returnBack', false)
}
function showAssetDetail(row) {
  assetDetailInfo.value.id = row.assetId
  assetDetailInfo.value.visible = true
}
function getMain() {
  getInventoryLossOrderById(props.orderId).then((resp) => {
    if (resp && resp.success) {
      infoList.value.data = resp.data
    }
  })
}
function getList() {
  getInventoryLossOrderDetailCustomizeListAll(filterInfo.value.query).then(
    (resp) => {
      if (resp && resp.success) {
        tableInfo.value.data = resp.data
      }
    }
  )
}
function returnModify() {
  getMain()
  getList()
}
//生成资产处置
function modifyAssetDel() {
  inventoryLossOrderValidateCanDeal(infoList.value.data.id).then((res) => {
    if (res && res.success) {
      if (res.data) {
        modifyAssetDelInfo.value.visible = true
        modifyAssetDelInfo.value.row = infoList.value.data
      } else {
        proxy.$message.error({
          message: '当前盘亏单不能生成资产处置单',
          duration: 3000,
          showClose: true,
        })
      }
    }
  })
}
const templateArr = ref([])
const popoverVisible = ref(false)
//打印
function print() {
  selectByCodeAndDept({
    tempCode: 'assetInventoryLossDetail',
  }).then((res) => {
    if (res && res.success) {
      templateArr.value = res.data
      popoverVisible.value = true
      // 一条数据则直接打印
      if (templateArr.value.length === 1) {
        proxy.$refs.popoverShow.hidePopover()
        doPrint({
          name: templateArr.value[0].ureportName,
          params: {
            orderId: props.orderId,
            orderNo: infoList.value.data.orderNo,
          },
          callback: printCallback,
          printCount: true,
          data: {
            customParam: JSON.stringify({
              ...filterInfo.value.query,
            }),
          },
        })
      }
    }
  })
}
function printCallback() {
  try {
  } catch (error) {}
}
function tempClick(item) {
  doPrint({
    name: item.ureportName,
    params: {
      orderId: props.orderId,
      orderNo: infoList.value.data.orderNo,
    },
    callback: printCallback,
    printCount: true,
    data: {},
  })
  popoverVisible.value = false
}

//导出excel
function exportExcel() {
  exportInventoryLossOrderDetail(filterInfo.value.query).then((resp) => {
    if (resp) {
      proxy.$fn.parseExcel(resp)
    }
  })
}
function getCustomize() {
  let param = {
    tableKey:
      'assetManageAssetInventoryLossDetail-com.slip.spd.entity.vo.asset.AssetInventoryLossOrderDetailVO',
  }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      filterInfo.value.query.customizeUserVO = data
      let obj = proxy.$fn.setFieldList(
        tableInfo.value.fieldList,
        data.detail,
        true
      )
      tableInfo.value.fieldList = obj.fieldList
      getMain()
      getList()
    }
  })
}

let obj = {
  assetName: {
    label: '资产名称',
    value: 'assetName',
    componentType: 'input',
    type: 'slot',
    show: false,
    hidden: true,
  },
  planOrderNo: {
    label: '计划单号',
    value: 'planOrderNo',
    componentType: 'input',
    show: false,
    hidden: true,
  },
}
tableInfo.value.fieldList = Object.values({
  ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
  ...obj,
})
getCustomize()
</script>

<style scoped></style>
